package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.DbUtil;
import com.csjy.migration.hnnd.ProgramArgument;
import com.csjy.migration.hnnd.Tool;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;

import javax.script.ScriptException;
import java.io.FileWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/*归档资料管理*/
@BusinessDataConverter(
        insertSql = "insert into nd_project_archive (ID, CREATE_NAME, CREATE_BY, CREATE_DATE, UPDATE_NAME, UPDATE_BY, UPDATE_DATE, SYS_ORG_CODE, SYS_COMPANY_CODE, BPM_STATUS, FILE_ENCODE, FILE_NAME, PRO_ENCODE, FILE_NUMBER, FILE_TYPE, ATTACHMENT_NAME, STORE_TYPE, CONTRACT_ENCODE, PAGE_REMARK, INFOS_ID, PRO_ID, FILE_STATUS, IS_VALID, IS_PIGEONHOLE, TIME_OF_FILING, ARCHIVE_CATEGORY, LETTER_NUM, PROC_ID)" +
                "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
        updateSql = "UPDATE nd_project_archive SET CREATE_NAME = ?,CREATE_BY = ?,CREATE_DATE = ?,UPDATE_NAME = ?,UPDATE_BY = ?,UPDATE_DATE = ?,SYS_ORG_CODE = ?,SYS_COMPANY_CODE = ?,BPM_STATUS = ?,FILE_ENCODE = ?,FILE_NAME = ?,PRO_ENCODE = ?,FILE_NUMBER = ?,FILE_TYPE = ?,ATTACHMENT_NAME = ?,STORE_TYPE = ?,CONTRACT_ENCODE = ?,PAGE_REMARK = ?,INFOS_ID = ?,PRO_ID = ?,FILE_STATUS = ?,IS_VALID = ?,IS_PIGEONHOLE = ?,TIME_OF_FILING = ?,ARCHIVE_CATEGORY = ?,LETTER_NUM = ?,PROC_ID = ? WHERE ID =?",
        findMigratedIdListSql = "SELECT ID FROM nd_project_archive WHERE PRO_ID = ?"
)
public class NdProjectArchive extends AbstractBusiness{


    public NdProjectArchive(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
        super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
    }

    @Override
    public void migrate() throws Exception {
        for(Map btitembasicdatumlist:sourceData.getBtItemBasicDatumList()) {
            try {
                String sourceId = btitembasicdatumlist.get("DOCNO").toString();
                int needMigratedResult = this.needMigrated(sourceId);
                if(needMigratedResult == 0) {
                    continue;
                };
                // 单条插入或更新
                if (needMigratedResult == 1) {
                    this.targetDbUtil.updateByParams(updateSql, this.buildValues(btitembasicdatumlist), true);
                    // 批量插入或更新 和 单条插入或更新 二选一
//             this.batchUpdateValues.add(this.buildValues(row));
                } else {
                    this.targetDbUtil.updateByParams(insertSql, this.buildValues(btitembasicdatumlist ));
                    // 批量插入或更新 和 单条插入或更新 二选一
//             this.batchInsertValues.add(this.buildValues(row));
                }

            } catch (Exception e) {
                throw new Exception(String.format("归档资料管理[%s]迁移失败:%s\n", btitembasicdatumlist.get("ITEMNO"), e.getMessage()));
            }
        }


        }


    public List buildValues (Map btitembasicdatumlist) throws  ScriptException {
        Map<String,String> fileTypeMap = new HashMap();
        fileTypeMap.put("1","一般档案");
        fileTypeMap.put("2","重要档案");
        fileTypeMap.put("3","法律文件");
        fileTypeMap.put("4","诉讼档案");

        List values = new ArrayList();
            //ID	主键
            values.add(btitembasicdatumlist.get("DOCNO").toString());
            //CREATE_NAME	创建人名称
            values.add(null);
            //CREATE_BY	创建人登录名称
            values.add(btitembasicdatumlist.get("EMPCODE"));
            //CREATE_DATE	创建日期
            values.add(btitembasicdatumlist.get("CREATE_TIME"));
            //UPDATE_NAME	更新人名称
            values.add(null);
            //UPDATE_BY	更新人登录名称
            values.add(null);
            //UPDATE_DATE	更新日期
            values.add(btitembasicdatumlist.get("MODIFY_TIME"));
            //SYS_ORG_CODE	所属部门
            values.add(null);
            //SYS_COMPANY_CODE	所属公司
            values.add(null);
            //BPM_STATUS	流程状态
            values.add(1);
            //FILE_ENCODE	档案编码
            values.add(btitembasicdatumlist.get("DOCCODE"));
            //FILE_NAME	档案名称
            values.add(btitembasicdatumlist.get("DOCNAME"));
            //PRO_ENCODE	项目编码
            values.add(sourceData.getBtItemBasic().get(0).get("ITEMCODE"));
            //FILE_NUMBER	档案页数
            values.add(btitembasicdatumlist.get("PAGESPERUNIT"));
            //FILE_TYPE	档案类型
            values.add(fileTypeMap.get(btitembasicdatumlist.get("DATUMTYPE").toString()));
            //ATTACHMENT_NAME	附件名称
            values.add(btitembasicdatumlist.get("ELEFILENAME"));
            //STORE_TYPE	存储形式
            values.add(btitembasicdatumlist.get("DOCTYPE").toString());
            //CONTRACT_ENCODE	合同编号
            values.add(btitembasicdatumlist.get("CONTRACTNO"));
            //PAGE_REMARK	页码位置文件备注
            values.add(btitembasicdatumlist.get("DOCREMARK") == null ? null : Tool.decodeUnicode(btitembasicdatumlist.get("DOCREMARK").toString()));
            //INFOS_ID	项目附件资料(模板)ID
            values.add(btitembasicdatumlist.get("ELEFILESTORENAME") == null ? null : btitembasicdatumlist.get("ELEFILESTORENAME").toString().replace("-",""));
            //PRO_ID	项目ID
            values.add(btitembasicdatumlist.get("ITEMNO"));
            //FILE_STATUS	文件状态(待补交)
            values.add("已提交");
            //        IS_VALID	是否使用
            values.add("Y");
            //IS_PIGEONHOLE	入档状态
            values.add(1);
            //TIME_OF_FILING	归档时间
            values.add(null);
            //ARCHIVE_CATEGORY	归档类别
            values.add(getArchiveCategory(btitembasicdatumlist.get("EMPCODE").toString()));
            //LETTER_NUM	序号编码
            values.add(null);
            //PROC_ID	流程ID
            values.add(btitembasicdatumlist.get("WORKFLOWNO") == null ? btitembasicdatumlist.get("WORKFLOWNO")+"-JW" : null);
            return values;
    }
    /**
     * 获取档案分类
     * @return
     */
    public java.lang.String getArchiveCategory(String empcode) {
        if(sourceData.getBtItemBasic().get(0).get("MANAGERA") == null) {
            return "B类";
        } else {
            return sourceData.getBtItemBasic().get(0).get("MANAGERA").toString().equals(empcode) ? "B类" : "A类";
        }
    }

}

